package cn.jiangxiong.pethome.system.dept.controller;

import cn.jiangxiong.pethome.system.dept.domain.Department;
import cn.jiangxiong.pethome.system.dept.query.DepartmentQuery;
import cn.jiangxiong.pethome.system.dept.service.IDepartmentService;
import cn.jiangxiong.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController{
    @Autowired
    private IDepartmentService departmentService;

   // @RequestMapping(value = "/list",method = RequestMethod.GET)Restuful方式一
   //@GetMapping("/list")   Restuful方式二
    @PostMapping("/list")   //方式二
    @ApiOperation(value = "获取所有不们信息")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门信息")
    public AjaxResult findOne(/*@ApiParam(name="ID属性",required = true)*/ @PathVariable Long id){
        try {
            Department one = departmentService.findOne(id);
            return AjaxResult.success().setData(one);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
   /*都是用来处理json数据的
    @RequestBody是前端传来的
    @ResponseBody是后端返回给前端的*/

    @PostMapping("/pagelist")
    @ApiOperation(value = "分页查询")
    public AjaxResult findQuery(@RequestBody DepartmentQuery query){
        try {
            List<Department> byQuery = departmentService.findByQuery(query);
            Long count = departmentService.findByQueryCount(query);
            return AjaxResult.success().setData(byQuery).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    public AjaxResult delete(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }


    @PutMapping
    @ApiOperation(value = "保存部门信息【有id则为修改】")
    public AjaxResult save(@RequestBody Department department) {
        try {
            if (department.getId() != null) {
                departmentService.update(department);

            } else {
                departmentService.add(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }

    }
    //批量删除
    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除部门")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Department> getDeptTree(){
        return departmentService.deptTree();
    }

}
